//
//  LTRecommendViewController.swift
//  myMoya
//
//  Created by lete on 2020/4/27.
//  Copyright © 2020 xiaoyi. All rights reserved.
//

import UIKit



class LTRecommendViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    
    var sidebarDataArr:[ShopSidebarModel] = [ShopSidebarModel]()
    
    lazy var mytableview: UITableView = {
        //创建 tableview
        let tableView = UITableView()
        
        //注册 cell
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
        //xib cell 的注册
        //        tableView.register(UINib(nibName: "UITableViewCell", bundle: nil), forCellReuseIdentifier: "cellId")
        
        // 设置代理
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        setupUI()
        
        //gif
//        mytableview.uHead = URefreshHeader{ [weak self] in self?.getNetDataMethod() }
        mytableview.uHead = URefreshNormalHeader{ [weak self] in self?.getNewNetDataMethod() }
        mytableview.uFoot = URefreshAutoFooter{ [weak self] in self?.getNetDataMethod() }

        self.mytableview.uHead.beginRefreshing()
        getNetDataMethod()
        
        
    }
    
    func setupUI() {
        view.backgroundColor = UIColor.white
        
        view.addSubview(mytableview)
        mytableview.snp.makeConstraints { (make) in
            make.top.bottom.left.right.equalToSuperview()
        }
        
        
    }
    
    
    //MARK: - Tableview Delegate
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.sidebarDataArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
        
        let siderModel = self.sidebarDataArr[indexPath.row]
        
        cell.textLabel?.text = "\(siderModel.name)"
        return cell
    }
    
    
    //MARK: - networking
    func getNewNetDataMethod() {
        self.sidebarDataArr.removeAll()
        getNetDataMethod()
    }
    func getNetDataMethod() {
        
        //MARK: - 网络请求
        LTNetwork.request(target: .goods(route: "app/getData", version: 1), success: { (result) in
            //            LTLog(result)
            
            //得到基础的结构数据
            guard let baseModel = NetBaseModel(JSON: (result as! [String : AnyObject])) else { return }
            LTLog("数据 msg: \(baseModel.status ?? 0)")
            LTLog("数据 msg: \(baseModel.text)")
            LTLog("数据 msg: \(baseModel.textstr)")
            //            LTLog("数据 msg: \(String(describing: baseModel.data))")
            
            
            //使用Mapper
            //把对象转成 model
            guard let shopModel = Mapper<ShopModel>().map(JSONObject: baseModel.data)  else{ return}
            LTLog(shopModel.serverTime)
            LTLog(shopModel.shop_config)
            LTLog("config======>> \(shopModel.shop_config?.keywords ?? "")")
            LTLog("banner======>> \(shopModel.banner)")
            
            //使用Mapper转模型数组
            //            //使用Mapper转模型数组
            //let users: [User] = Mapper<User>().mapArray(JSONString: JSONString)
            
            //从 model 中直接取值
            let bannerModelArr: [ShopBannerModel] = shopModel.banner
            LTLog(bannerModelArr)
            
            LTLog("banner 名字=====>>> \(String(describing: bannerModelArr[0].name))")
            
            let sidebarArr: [ShopSidebarModel] = shopModel.sidebar
            
            self.sidebarDataArr = self.sidebarDataArr + sidebarArr
            
            
            self.mytableview.uHead.endRefreshing()
            
            
            guard self.sidebarDataArr.count < 100
                else{
                    self.mytableview.uFoot.endRefreshingWithNoMoreData()
                    return
                    }
            self.mytableview.uFoot.endRefreshing()
            
            
                       // 更新列表数据
                       self.mytableview.reloadData()
          
            
           
            
        }) { (error) in
            LTLog(error.localizedDescription)
            self.mytableview.uHead.endRefreshing()
            self.mytableview.uFoot.endRefreshing()

        }
    }
    
    
}
